﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 第2次课_EF
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1 创建数据上下文（增删改查）
            Model1 model1 = new Model1();
            // 2 属性导航
            var task = model1.Task;

            // 3 数据操作
            //// 3.1数据操作——查询
            Select(model1);
            //Select2(model1);
            //// 3.2数据操作——添加
            Add(model1);
            //// 3.3数据操作——删除
            //Del(model1);
            Del2(model1);
            //// 3.4数据操作——修改
            //Update(model1);

            

        }
        /// <summary>
        /// 3.1数据操作——查询
        /// </summary>
        static void Select(Model1 model1)
        {
            var list = model1.Task.ToList();
          
            foreach (var item in list)
            {
                Console.WriteLine(item.TaskID + "  " + item.Content);   
            }
            Console.WriteLine();
        }
        /// <summary>
        /// 3.1.1数据操作——条件查询
        /// </summary>
        static void Select2(Model1 model1)
        {
            var list = model1.Task.Where(p => p.Content.Contains("实现")).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.TaskID + "  " + item.Content);
            }
        }
        /// <summary>
        /// 3.2数据操作——添加
        /// </summary>
        /// <param name="model1"></param>
        static void Add(Model1 model1)
        {
            var t = new Task()
            {
                AddTime = DateTime.Now,
                Content = "实现添加功能",
                TaskPriority = 1,
                uid = 1,
                TaskState = 1
            };
            model1.Task.Add(t);
            //保存至数据库
            model1.SaveChanges();
            Select(model1);
        }
        /// <summary>
        /// 3.3数据操作——删除
        /// </summary>
        /// <param name="model1"></param>
        static void Del(Model1 model1)
        {
            var d = model1.Task.FirstOrDefault(p => p.TaskID == 11);
            if (d != null)
            {
                model1.Task.Remove(d);
                model1.SaveChanges();
                Select(model1);
            }
        }
        /// <summary>
        /// 3.3数据操作——删除
        /// </summary>
        /// <param name="model1"></param>
        static void Del2(Model1 model1)
        {
            var d = model1.Task.Last();
            if (d != null)
            {
                model1.Task.Remove(d);
                model1.SaveChanges();
                Select(model1);
            }
        }
        /// <summary>
        /// 3.4数据操作——修改
        /// </summary>
        /// <param name="model1"></param>
        static void Update(Model1 model1)
        {
            var u = model1.Task.FirstOrDefault(p => p.TaskID == 11);
            if (u != null)
            {
                u.TaskState = 2;
                u.FinishedTime = DateTime.Now;
                model1.SaveChanges();
                Select(model1);
            }
        }
    }
}
